package com.example.game1.managers;

import com.example.game1.MainActivity;
import com.example.game1.rooms.Room;

public class GridManager 
{
	private int mWidth, mHeight;
	private Room mRoom;
	
	private static GridManager mInstance;
	private MainActivity mContext;
	
	// A single grid will be 45x45 px square
	public static final int SINGLE_GRID_SIZE = 45;
	
	private GridManager(MainActivity context, int width,int height)
	{
		mWidth=width;
		mHeight=height;
		mContext = context;
		//Making grid
		
		
	}
	
	synchronized public static void init(MainActivity context, int width, int height)
	{
		
		
		// Guarantees that only one grid manager can exist 
		if (mInstance == null)
			mInstance = new GridManager(context, width, height);
		
		
	}
	
	synchronized public static GridManager getInstance()
	{
		if (mInstance == null)
			throw new IllegalAccessError("GridManager not initialized!");
		
		return mInstance;
	}
	
	public int[] convertPixelsToGrid(float x,float y)
	{
		int gridX = (int) (x / SINGLE_GRID_SIZE);
		int gridY = (int) (y / SINGLE_GRID_SIZE);
		
		return new int[]{ gridX, gridY };
	}
	
	public int[] convertGridToPixels(int x, int y)
	{
		/* Returns the first pixel in the grid.
		 * 
		 * So if this is our single grid space:
		 * 
		 * X--------------
		 * -             -
		 * -             -
		 * -             -
		 * -             -
		 * -             -
		 * ---------------
		 * 
		 * it will return pixel X
		 * 
		 */
		
		int pixelsX = x * SINGLE_GRID_SIZE;
		int pixelsY = y * SINGLE_GRID_SIZE;
		
		return new int[]{ pixelsX, pixelsY };
	}
	
	public void setRoom(Room r)
	{
		mRoom = r;
		
		// Pass the room to the DrawingSurface so it can load the room resources into memory
		mContext.mDrawingSurface.loadNewResources(r);
	}
	
	public Room getRoom()
	{
		return mRoom;
	}
}
